var v_row=[] ;
var ss=-1;

function compareValue(){
	console.log(v_row[ss].url);
}

$(function(){
	
	
	//专业的下拉列表
	/*$('#major').combobox({
        valueField:'specialty_id',
         textField:'specialty_name',
         url:'major.do',
         method:'get',
         onSelect:function(rec){
          var url1='curriculum.do?fid='+rec.specialty_id;
          $('#curriculum').combobox('reload',url1);
        },
        onLoadSuccess: function () {  //加载完成后,设置选中第一项
            var val = $(this).combobox('getData');
             console.log(val);
             alert(val[0].specialty_name);
             
            for(var item in val[0]) {
            	console.log(item);
                $(this).combobox('select',val[0][item]);
            }
            $(this).combobox('select',val[0].specialty_name);
        }
   });
	//课程的下拉列表
	$('#curriculum').combobox({
       valueField:'specialty_id',
        textField:'specialty_name',
        method:'get',
        onSelect:function(rec){
         var url1='chapter.do?fid='+rec.specialty_id;
         $('#chapter').combobox('reload',url1);
        },
       onLoadSuccess: function () {  //加载完成后,设置选中第一项
         var val = $(this).combobox('getData');
         for(var item in val[0]) {
             $(this).combobox('select', val[0][item]);
         }
         $(this).combobox('select',val[0].specialty_name);
       }
   });
	//章节的下拉列表
   $('#chapter').combobox({
        valueField:'specialty_id',
         textField:'specialty_name',
         onLoadSuccess: function () {  //加载完成后,设置选中第一项
          var val = $(this).combobox('getData');
          for(var item in val[0]) {
               $(this).combobox('select', val[0][item]);
          }
          $(this).combobox('select',val[0].specialty_name);
        }
   });*/
   
	//树形
   $('#tt').tree({    
	    url:'tree.do',
	    loadFilter: function(rows){
	    	
			return convert(rows);
		}/*,
		  onLoadSuccess:function(node,data){
			alert(111);
			var t = $(this);
			if(data){
			       $(data).each(function(index,d){
					   if(this.state == 'closed'){
					       t.tree('expandAll');
					   }
			       });
			  }
		 }*/
	});
   function convert(rows){
		function exists(rows, parentId){
			for(var i=0; i<rows.length; i++){
				if (rows[i].specialty_id == parentId) 
					return true;
			}
			return false;
		}
		
		var nodes = [];
		// 获取顶级节点
		for(var i=0; i<rows.length; i++){
			var row = rows[i];
			if (!exists(rows, row.parentId)){
				nodes.push({
					id:row.specialty_id,
					text:row.specialty_name,
					
				});
			}
		}
		
		
		var toDo = [];
		for(var i=0; i<nodes.length; i++){
			toDo.push(nodes[i]);
		}
		while(toDo.length){
			var node = toDo.shift();	// 父節點
			
			//獲取子節點 
			for(var i=0; i<rows.length; i++){
				var row = rows[i];
				if (row.parentId == node.id){
					var child = {id:row.specialty_id,text:row.specialty_name};
					
					if (node.children){
						node.children.push(child);
					} else {
						node.children = [child];
					}
					toDo.push(child);
				}
			}
		}
		return nodes;
	}
	//選擇文件時
   $('#tt').tree({    
	   onSelect:function(none){
		   
		   $("#p1").attr("style","display:none;");//隐藏div
		   var cid=none.id;
		    aa(cid);
	   }
	});
   
   
   
   function aa(cid){
	   
	   $("#p2").attr("style","display:block;");//隐藏div
	   
	   $('#dg').datagrid({    
		    url:'datagrid?cid='+cid,
		    singleSelect:true,
		    columns:[[    
		    	{
					field:"curriculum_id",
					title:"教程ID",
					width:80,
				},
				{
					field:"user",
					title:"上传老师",
					width:200,
					formatter:function(value,row,index){
						
						var bb = value.user_name;
						return bb;
					}
				},
				{
					field:"curriculum_createtime",
					title:"上传时间",
					width:300,
					formatter : crtTimeFtt
				},
				{
					field:"upload_state",
					title:"文件类型",
					width:100,
				},
				{
					field:"curriculum_name",
					title:"教程名称",
					width:300,
				},
				{
	                title : '操作',
	                field : 'url',
	                align:'center',
	                width:80,
	                
	                formatter: function(value,row,index){
	                	console.log(row.curriculum_id);
	                	v_row.push(row);
	                	ss++;
	                	var str = "<a href='download?filename="+row.curriculum_name+"&filetype="+row.upload_state+"' >下载</a>";  
	                    return str;
	                    //onClick='compareValue("+ss+")'
	                }
	            }
				
		    ]]  ,
			success:function(data){
				if (data=="-1") {
					$.messager.progress({
						text:"没有你所要的数据",
					})
				}
			}
	 
		}); 
	   
	  
   };
  
   //时间格式化的方法
	  function crtTimeFtt(val, row) {
			 if (val != null) {
			   var date = new Date(val);
			   return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()+'         '+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
			  }
		}
   
 
})